package com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.Gps18DeviceBindTruck;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.Gps18TruckOverspeed;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.Gps18DeviceBindTruckMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.IGps18DeviceBindTruckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.Gps18DeviceBindTruckVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.Gps18DeviceListVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lvNuo
 * @since 2025-04-02
 */
@Service
public class Gps18DeviceBindTruckServiceImpl extends ServiceImpl<Gps18DeviceBindTruckMapper, Gps18DeviceBindTruck> implements IGps18DeviceBindTruckService {
    @Autowired
    private Gps18TruckOverspeedServiceImpl gps18TruckOverspeedService;

    @Override
    public SzResult getList(Integer page, Integer pageSize, Integer inuse,String truckNo,String startTime,String endTime,Integer macNumber) {
        QueryWrapper<Gps18DeviceBindTruck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("INUSE",inuse);
        queryWrapper.like(StringUtils.isNotEmpty(truckNo),"TRUCK_NO",truckNo);
        queryWrapper.ge(StringUtils.isNotEmpty(startTime),"BIND_TIME",startTime);
        queryWrapper.le(StringUtils.isNotEmpty(endTime),"BIND_TIME",endTime);
        queryWrapper.eq(macNumber != null,"MAC_NUMBER",macNumber);
        Page<Gps18DeviceBindTruck> iPage = page(new Page<Gps18DeviceBindTruck>(page, pageSize), queryWrapper);
        List<Gps18DeviceBindTruckVo> list = iPage.getRecords().stream().map(item->{
            Gps18DeviceBindTruckVo gps18DeviceBindTruckVo = new Gps18DeviceBindTruckVo();
            BeanUtil.copyProperties(item,gps18DeviceBindTruckVo);
            QueryWrapper<Gps18TruckOverspeed> gps18TruckOverspeedQueryWrapper = new QueryWrapper<>();
            gps18TruckOverspeedQueryWrapper.eq("BIND_ID",item.getId());
            int size = gps18TruckOverspeedService.list(gps18TruckOverspeedQueryWrapper).size();
            gps18DeviceBindTruckVo.setOverSpeedNum(size);
            return gps18DeviceBindTruckVo;
        }).collect(Collectors.toList());
        Long total = iPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", list);
        return SzResult.success(map);
    }
}
